package com.wish.finance.controller;

import cn.hutool.core.util.RandomUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wish.base.dto.CostType;
import com.wish.base.dto.PayManageDto;
import com.wish.base.dto.TCwPaymentPlanDto;
import com.wish.base.entity.CwPaymentPlan;
import com.wish.base.service.ICwPaymentPlanService;
import com.wish.base.service.ISfContractService;
import com.wish.common.core.web.controller.BaseController;
import com.wish.common.core.web.domain.AjaxResult;
import com.wish.common.core.web.page.TableDataInfo;
import com.wish.common.security.annotation.PreAuthorize;
import com.wish.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 【请填写功能名称】Controller
 * 
 * @author wish
 * @date 2022-06-05
 */
@RestController
@RequestMapping("/payment_plan")
public class TCwPaymentPlanController extends BaseController
{
    @Autowired
    ICwPaymentPlanService cwPaymentPlanService;
    @Autowired
    ISfContractService sfContractService;

    /**
     * 修改付款
     * @param cwPaymentPlanDto
     * @return
     */
    @PostMapping("updatePayment")
    public AjaxResult updatePayment(@RequestBody TCwPaymentPlanDto cwPaymentPlanDto) {
        //修改数据
        CwPaymentPlan cwPaymentPlan = cwPaymentPlanService.getById(cwPaymentPlanDto.getId());
        cwPaymentPlan.setId(cwPaymentPlanDto.getId());
        cwPaymentPlan.setPayMethod(cwPaymentPlanDto.getPayMethod());
        cwPaymentPlan.setPayState(2);
        cwPaymentPlan.setPaidTime(new Date());
        cwPaymentPlan.setRealPaymentAmount(cwPaymentPlan.getPaymentAmount());
        cwPaymentPlan.setUpdateTime(new Date());
        cwPaymentPlanService.saveOrUpdate(cwPaymentPlan);
        return AjaxResult.success();
    }


    /**
     * 查询付款计划列表
     */
    @PreAuthorize(hasPermi = "finance:payment_plan:list")
    @GetMapping("/list")
    public TableDataInfo list(TCwPaymentPlanDto cwPaymentPlanDto)
    {
        startPage();
        List<PayManageDto> list = cwPaymentPlanService.paymentList(cwPaymentPlanDto);
        return getDataTable(list);
    }

    //添加方法
    /*@PostMapping("save")
    public void save(@RequestBody TCwPaymentPlanDto dto) {
        ArrayList<CostType> costTypes = dto.getCostTypes();
        Long id = null;
        for (CostType costType : costTypes) {
            dto.setId(id);
            //生成随机单号
            String s = RandomUtil.randomStringUpper(10);
            dto.setPayOrderNo(s);
            dto.setItemType(costType.getItemType());
            dto.setPaymentAmount(costType.getPaymentAmount());
            dto.setRealPaymentAmount(costType.getRealPaymentAmount());
            dto.setCreateUserid(SecurityUtils.getUserId());
            dto.setCreateTime(new Date());
            itCwPaymentPlanService.save(dto);
        }
    }*/

}
